using System;
using System.Globalization;

namespace Prometheus.Binding.ReadOnly.BaseConverters
{
    public class proCurrencyConverter : proChainConverter
    {
        public override Type ThisType
        {
            get { return typeof (decimal); }
        }

        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is decimal)
            {
                return ((decimal) value).ToString("c", culture);
            }
            return value;
        }

        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var s = value as string;
            if (s != null)
            {
                decimal val;
                if (decimal.TryParse(s, NumberStyles.Any, culture, out val))
                {
                    return val;
                }
            }
            return value;
        }
    }
}